package com.example.demoback.service;

import com.example.demoback.entity.Item;
import com.example.demoback.mapper.ItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ItemService {

    @Autowired
    ItemMapper mapper;
    @Autowired
    private ItemMapper itemMapper;

    public Item save (Item item) throws ServiceException{
//        验证用户输入
        if(item.getItemname()==null||item.getItemname().isBlank()){
            throw new ServiceException("请输入商品名");
        }
//        if(item.getSubcategoryID()<30){
//            throw new ServiceException("请选择正确的分类");
//        }
         if(item.getPrice()<=0){
            throw new ServiceException("请输入商品价格");
        }
        if(item.getDescription()==null||item.getDescription().isBlank()){
            throw new ServiceException("请输入商品描述");
        }
//        if (item.getCondition()==null||item.getCondition().isBlank()){
//            throw new ServiceException("请选择商品品相");
//        }

        //同名验证：判断是否已经录入该商品
        //查询条件构建器
//        LambdaQueryWrapper<Item> lqw = new LambdaQueryWrapper<Item>();
//        //name = ?
//        lqw.eq(Item::getItemName,item.getItemName());
//        Long cnt = itemMapper.selectCount(lqw);
//        if(cnt>0){
//            throw new ServiceException("该商品已经录入");
//        }
        itemMapper.insertOrUpdate(item);

        //保存
        if(item.getBrand()==null||item.getBrand().isBlank()){
            throw new ServiceException("请输入商品品牌");
        }
        return item;
    }
}
